package com.example.tianling;

import com.example.tianling.common.utils.ScheduledTask;
import com.example.tianling.entity.RoomUserEntity;
import com.example.tianling.service.Impl.MatchServiceImpl;
import com.example.tianling.service.Impl.RoomServiceImpl;
import com.example.tianling.service.Impl.RoomUserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;

import java.util.Date;
import java.util.List;

@SpringBootApplication
//@EnableAsync(proxyTargetClass=true)
public class TianLingApplication {

    private static ScheduledTask scheduledTask;

    private static RoomServiceImpl roomService;

    private static MatchServiceImpl matchService;

    private static RoomUserServiceImpl roomUserService;

    @Autowired
    public void setScheduledTask(ScheduledTask scheduledTask) {
        TianLingApplication.scheduledTask = scheduledTask;
    }

    @Autowired
    public void setMatchService(MatchServiceImpl matchService) {
        TianLingApplication.matchService = matchService;
    }

    @Autowired
    public void setRoomService(RoomServiceImpl roomService) {
        TianLingApplication.roomService = roomService;
    }

    @Autowired
    public void setRoomUserService(RoomUserServiceImpl roomUserService) {
        TianLingApplication.roomUserService = roomUserService;
    }

    public static void main(String[] args) {
        SpringApplication.run(TianLingApplication.class, args);

//        scheduledTask.CyclicScheduledTaskBySecond(5, new Runnable() {
//            @Override
//            public void run() {
//                List<RoomUserEntity> roomUserList = roomUserService.getRoomUserByUserState(-1);
//                if (roomUserList != null) {
//                    Date nowDate = new Date();
//                    for (RoomUserEntity roomUser : roomUserList) {
//                        if ((nowDate.getTime() - roomUser.getTime().getTime()) > 5000) {
//                            roomService.reduceRealNumberByRoomId(roomUser.getRoomId());
//                            matchService.reduceRealNumberByRoomId(roomUser.getRoomId());
//                            roomUserService.removeUserByUserId(roomUser.getUserId());
//                        }
//                    }
//                }
//            }
//        });
    }

}
